
TARGET=freertos_task


BUILD_DIR=build

##### gcc
CROSS_COMPILE ?=arm-none-eabi-

CC=$(CROSS_COMPILE)gcc
AS=$(CROSS_COMPILE)gcc -x assembler-with-cpp
CP=$(CROSS_COMPILE)objcopy
SZ=$(CROSS_COMPILE)size
LD=$(CROSS_COMPILE)ld
DP=$(CROSS_COMPILE)objdump


PICO_LIB_PATH=pico-sdk/src
TOOLS_PATH=$(PICO_LIB_PATH)/tools

include $(PICO_LIB_PATH)/pico_inc.mk
include $(PICO_LIB_PATH)/pico_lib_src.mk

#boot2_stage checksum 文件
BS2_CHECK_S=$(BUILD_DIR)/bs2_default_padded_checksummed.S

SRC_C+=$(PICO_LIB_SRC_C)
#### src files
 
SRC_C+=$(wildcard src/*.c)

### libc newlib picolib llvm_libc 选择一个
SRC_C+= $(PICO_LIB_PATH)/rp2_common/pico_clib_interface/newlib_interface.c


#freertos dir
FREE_RTOS_DIR=FreeRTOS-Kernel
FREE_RTOS_SRC_C+=$(wildcard $(FREE_RTOS_DIR)/*.c)
FREE_RTOS_SRC_C+=$(FREE_RTOS_DIR)/portable/MemMang/heap_4.c 
FREE_RTOS_SRC_C+=$(wildcard $(FREE_RTOS_DIR)/portable/ThirdParty/GCC/RP2040/*.c)
FREE_RTOS_SRC_C+=$(FREE_RTOS_PICO_LIB_SRC_C)


SRC_CXX+=$(PICO_LIB_SRC_CXX)

SRC_S+=$(PICO_LIB_SRC_S)
SRC_S+=$(BS2_CHECK_S)
 
SRC_LD=$(PICO_LIB_LD)

#### .o files
OBJ_C=$(addprefix $(BUILD_DIR)/, $(SRC_C:.c=.o))
FREE_RTOS_OBJ_C=$(addprefix $(BUILD_DIR)/,  $(FREE_RTOS_SRC_C:.c=.o))
OBJ_S=$(addprefix $(BUILD_DIR)/,  $(SRC_S:.S=.o))
OBJ_CXX=$(addprefix $(BUILD_DIR)/, $(SRC_CXX:.cpp=.o))

#### include dir
INC=-Iinc -I. \
-I$(FREE_RTOS_DIR)/include \
-I$(FREE_RTOS_DIR)/portable/ThirdParty/GCC/RP2040/include
# boot_stage2
INC+=-I$(PICO_LIB_PATH)/rp2040/boot_stage2/include
INC+=$(PICO_LIB_INCLUDE)


DEFS+= \
-DPICO_BOARD=\"WEACT_STUDIO_RP2040_16MB\" \
-DLIB_PICO_STDIO=1 \
-DLIB_PICO_STDIO_UART=1 \
-DLIB_PICO_STDIO_USB=0 \
-DFREE_RTOS_KERNEL_SMP=1 \
-DconfigNUMBER_OF_CORES=2 \
-DLIB_PICO_MULTICORE

#### CFLAGS-DPICO_TARGET_NAME=$(TARGET) \ -DPICO_CMAKE_BUILD_TYPE=Release \ -DPICO_COPY_TO_RAM=0 \

DEFS+= \
-DPICO_RP2040=1 \
-DLIB_BOOT_STAGE2_HEADERS=1 \
-DLIB_PICO_DOUBLE=1 \
-DPICO_NO_FLASH=0 \
-DLIB_PICO_PRINTF_PICO=1 \
-DPICO_32BIT=1 \
-DPICO_CXX_ENABLE_EXCEPTIONS=0 \
-DPICO_ON_DEVICE=1 

 

CFLAGS+= \
-mcpu=cortex-m0plus \
-mthumb \
$(DEFS) \
-Wall \
-fdata-sections \
-ffunction-sections \
-Og \
-g \
-Wno-return-type

CXXFLAGS+= \
-mcpu=cortex-m0plus \
-mthumb \
$(DEFS) \
-Wall \
-fdata-sections \
-ffunction-sections \
-Og \
-g 

#-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst))
#-g \
#-gdwarf-2 \
#-MMD -MP -MF"$(@:%.o=%.d)"


#### LINK 
LIBS= \
#-lc -lm -lnosys

LDFLAGS =-mcpu=cortex-m0plus \
-Wl,--gc-sections \
-specs=nano.specs -T$(SRC_LD) \
$(LIBS) \
-Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref


### .c path set
vpath %.c $(sort $(dir $(SRC_C) ))
vpath %.c $(sort $(dir $(FREE_RTOS_SRC_C) ))
vpath %.S $(sort $(dir $(SRC_S) ))
vpath %.cpp $(sort $(dir $(SRC_CXX) ))
 
ELF=$(BUILD_DIR)/$(TARGET).elf
BIN=$(BUILD_DIR)/$(TARGET).bin
DIS=$(BUILD_DIR)/$(TARGET).dis
UF2=$(BUILD_DIR)/$(TARGET).uf2



#### build
.PHONY:all clean  test dis d c bs2 bs2_clean 

all: $(BUILD_DIR) $(BIN) $(DIS) $(bs2) $(UF2)
	@echo all done.

########################################BS2 flash 引导2 开始 ####################################

## boot2 stage > bs2_default_padded_checksummed.S
BS2_S_NAME  = compile_time_choice
#BS2_S_NAME  = boot2_w25q080 

BS2_DEFS = -DLIB_BOOT_STAGE2_HEADERS=1 -DPICO_32BIT=1 -DPICO_BOARD=\"pico\" -DPICO_BUILD=1 -DPICO_NO_HARDWARE=0 -DPICO_ON_DEVICE=1 -DPICO_RP2040=1

BS2_S+=$(PICO_LIB_PATH)/rp2040/boot_stage2/$(BS2_S_NAME).S  
BS2_OBJ+=$(BUILD_DIR)/$(BS2_S_NAME).o
 

#BS2_INC=-I$(PICO_LIB_PATH)/rp2040/boot_stage2/asminclude -I$(PICO_LIB_PATH)/rp2040/hardware_regs/include -I$(PICO_LIB_PATH)/rp2_common/hardware_base/include -I$(PICO_LIB_PATH)/common/pico_base_headers/include -ID:/ESP/rp/pico-examples/bin/generated/pico_base -I$(PICO_LIB_PATH)/boards/include -I$(PICO_LIB_PATH)/rp2040/pico_platform/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_compiler/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_panic/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_sections/include -I$(PICO_LIB_PATH)/rp2040/boot_stage2/include

#BS2_INC=-I$(PICO_LIB_PATH)/rp2040/boot_stage2/asminclude -I$(PICO_LIB_PATH)/rp2040/hardware_regs/include -I$(PICO_LIB_PATH)/rp2_common/hardware_base/include -I$(PICO_LIB_PATH)/common/pico_base_headers/include -ID:/ESP/rp/pico-examples/bin/generated/pico_base -I$(PICO_LIB_PATH)/boards/include -I$(PICO_LIB_PATH)/rp2040/pico_platform/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_compiler/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_panic/include -I$(PICO_LIB_PATH)/rp2_common/pico_platform_sections/include -I$(PICO_LIB_PATH)/rp2040/boot_stage2/include

BS2_INC+=-Iinc   
BS2_INC+=$(PICO_LIB_BS2_INC) 
 
BS2_LD=$(PICO_LIB_PATH)/rp2040/boot_stage2/boot_stage2.ld

BS2_CFLAGS+= $(BS2_DEFS) \
-mcpu=cortex-m0plus \
-mthumb \
-g -O3 -DNDEBUG 

BS2_LD_FLAGS=-mcpu=cortex-m0plus -mthumb \
-T$(BS2_LD) \
-Wl,-Map=$(BUILD_DIR)/bs2_default.map \
-nostartfiles  \
-Wl,--build-id=none \
--specs=nosys.specs \
-g -O3 -DNDEBUG \
 

BS2_ELF=$(BUILD_DIR)/bs2_default.elf
BS2_DIS=$(BUILD_DIR)/bs2_default.dis
BS2_BIN=$(BUILD_DIR)/bs2_default.bin

 


bs2:$(BUILD_DIR)  $(BS2_DIS) $(BS2_CHECK_S)
	@echo bs2

$(BS2_OBJ):$(BS2_S)
	@echo ---------------Compiling bs2---------------
	@echo AS $<
	$(AS) -c $(BS2_CFLAGS)   $< $(BS2_INC) -o $@

$(BS2_ELF):$(BS2_OBJ)
	@$(CC) $(BS2_OBJ) $(BS2_LD_FLAGS) $(BS2_INC) -o $@
	@$(SZ) $@
 
# .elf -> .bin
$(BS2_BIN):$(BS2_ELF)
	@echo $@
	@$(CP) -O binary -S $< $@

#.elf -> dis
$(BS2_DIS):$(BS2_ELF)
	@echo  $@
	@$(DP) -DS $< > $@

$(BS2_CHECK_S):$(BS2_BIN)
	python $(PICO_LIB_PATH)/rp2040/boot_stage2/pad_checksum -s 0xffffffff $< $@

bs2_clean:
	rm -rf $(BS2_OBJ) $(BS2_ELF) $(BS2_DIS) $(BS2_BIN) $(BUILD_DIR)/bs2_default.map


help:
	@echo make all
	@echo make clean 
	@echo make bs2
	@echo make bs2_clean
	@echo make c 
	@echo make d



########################################BS2 flash 引导2 结束 ####################################



######################################## WROK TARGET ###################
# mkdir 
$(BUILD_DIR):
	mkdir $@
	 

# link elf
$(ELF):$(bs2) $(OBJ_C) $(OBJ_S) $(OBJ_CXX) $(FREE_RTOS_OBJ_C)
	@echo  ld $@
	@$(CC) $(OBJ_C) $(OBJ_S) $(FREE_RTOS_OBJ_C) $(OBJ_CXX) $(LDFLAGS) -I. -o $@
	@$(SZ) $@
	
# .c -> .o
$(BUILD_DIR)/%.o : %.c
	@echo CC $<
	@mkdir -p $(@D)
	@$(CC) -c $(CFLAGS) $< $(INC) -o $@

# .c -> .o
# $(BUILD_DIR)/freertos/%.o : %.c
# 	@echo CC FreeRTOS $<
# 	@$(CC) -c $(CFLAGS) $< $(INC) -o $@	

# .s -> .o
$(BUILD_DIR)/%.o : %.S
	@echo AS $<
	@mkdir -p $(@D)
	@$(AS) -c $(CFLAGS) $< $(INC) -o $@

# .cpp -> .o
$(BUILD_DIR)/%.o : %.cpp
	@echo CC $<
	@mkdir -p $(@D)
	@$(CC) -c $(CXXFLAGS)  $< $(INC) -o $@


# .elf -> .bin
$(BIN):$(ELF)
	@echo $@
	@$(CP) -O binary -S $< $@

#.elf -> dis
$(DIS):$(ELF)
	@echo  $@
	@$(DP) -DS $< > $@



$(UF2):$(ELF)
	@echo uf2 convert wit picotool
	@../../tools/picotool uf2 convert  $(ELF)  $(UF2) 
# 	../../tools/picotool uf2 convert  --verbose  $(ELF)  $(UF2) 
# 	@echo make elf2uf2.exe
# 	@make -C $(TOOLS_PATH)/uf2
# 	$(TOOLS_PATH)/uf2/elf2uf2 $< $@

clean:
	rm -rf $(BUILD_DIR)

dis:$(BUILD_DIR)/$(TARGET).elf
	$(DP) -DSh $< > $(BUILD_DIR)/$(TARGET).dis

test:
#	@echo $(SRC_C) 
	@echo $(OBJ_C)  

c:
	make clean

d: 
	make all -j12

#-include $(wildcard $(BUILD_DIR)/*.d)


download:
#  openocd 下载 -s 指定默认目录 -f 都在此目录下工作可以省略前缀
#  program 编程 
	../../tools/openocd-rpxx/openocd.exe \
	-s ../../tools/openocd-rpxx/scripts \
	-f interface/cmsis-dap.cfg \
	-f target/rp2040.cfg \
	-c "program $(ELF) verify reset" \
	-c "reset run" \
	-c exit

 



 